﻿using System;
using System.Collections.Generic;
using System.Linq;

using btmerc.API.Enums;
using btmerc.API.Schemas.Interfaces;

namespace btmerc.API.Schemas
{
    partial class ForceCharacter : IForceCharacter
    {
        // TODO: in salaryModifiers.xml, Scouts and Warships are not handled
        public List<Skill> PrimarySkills { get; private set; }

        public Troop Troop { get; set; }

        public Quality GetQuality(TroopType type)
        {
            // TODO: implement
            throw new NotImplementedException();
        }

        public double GetSalary(IEra era, SalaryModifiers salaryModifiers)
        {
            BaseSalary baseSalary = salaryModifiers.BaseSalaries.Where(bs => bs.TroopType == Troop.Type).FirstOrDefault();
            if (baseSalary == null)
            {
                throw new ArgumentOutOfRangeException("This troop type does not have a base salary set!");
            }

            QualityMultiplier qualityMultiplier = salaryModifiers.QualityMultipliers.Where(qm => qm.Quality == GetQuality(Troop.Type)).FirstOrDefault();
            if (qualityMultiplier == null)
            {
                throw new ArgumentOutOfRangeException("This quality does not have a multiplier set!");
            }

            Rank rank = GetRank();
            float rankMultiplier = rank.IsOfficer ? salaryModifiers.RankMultipliers.Officer.SalaryMultiplier : salaryModifiers.RankMultipliers.Enlisted.SalaryMultiplier;
            rankMultiplier *= rank.Grade / 2;

            return baseSalary.BaseSalary1 * qualityMultiplier.SalaryMultiplier * rankMultiplier * era.SalaryMultiplier;
        }

        public Rank GetRank()
        {
            bool isOfficer = Traits.Trait.Where(t => t.Name == "Commission").Any();
            ushort rank = Traits.Trait.Where(t => t.Name == "Rank").First().Value;
            return new Rank(isOfficer, rank);
        }

        public ushort GetWeeklyEffectiveHours(TroopType type)
        {
            switch (GetQuality(type))
            {
                case Quality.Astech:
                    return 20;
                case Quality.Green:
                    return 30;
                case Quality.Regular:
                    return 40;
                case Quality.Veteran:
                    return 45;
                case Quality.Elite:
                    return 50;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}
